// pages/appointment/appointmentDetail/appointmentDetail.js
import { RES_HOST } from "../../../utils/const"
import http from "../../../utils/http"
import util from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_fang_text: false,
    //页面数据
    detailData: '',
    id: '',
    res_host: RES_HOST,
    message: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    if (options.tasklist == "true") {
      this.setData({
        id: id,
        is_fang_text: false
      })
    } else {
      this.setData({
        id: id,
        is_fang_text: true
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function (options) {
    this.getDetail(this.data.id);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  //请求数据
  getDetail: function (id) {
    let that = this;
    http({
      url: "/look/queryInfoDetail/v1.0.0",
      method: "POST",
      data: {
        id: id
      },
      success: function (res) {
        if (res.code == 200) {
          res.data.startTime = util.getMonthDay(res.data.startTime)
          that.setData({
            detailData: res.data
          })
        } else {
          console.log("请求失败");
        }
      }
    })
  },
  //完成点击
  success_fang: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '您已经完成看房了吗？',
      confirmText: '完成看房',
      success: function (res) {
        if (res.confirm) {
          that.getSuccess(id);
        } else {
          return false
        }
      }
    });
  },
  //完成看房数据请求
  getSuccess: function (id) {
    let that = this;
    let key = "detailData.finishTime"
    http({
      url: "/look/updateInfo/v1.0.0look",
      method: "POST",
      data: {
        id: id,
        status: 1
      },
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            is_fang_text: true,
            [key]: res.data.finishTime
          })
        } else if (res.code == 403) {
          wx.showModal({
            title: "提示",
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.clearStorageSync("token")
                wx.clearStorageSync("isLoginStatus");
                wx.clearStorageSync("phoneNum");
                wx.navigateTo({
                  url: 'pages/my/login/login'
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: "提示",
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.clearStorageSync("token")
                wx.clearStorageSync("isLoginStatus");
                wx.clearStorageSync("phoneNum");
                wx.navigateTo({
                  url: 'pages/my/login/login'
                })
              }
            }
          })
        }
      }
    })
  },
  wuye_fang: function () {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.detailData.contactPhone
    });
  },
  messageShow: function () {
    let that = this;
    setTimeout(function () {
      that.setData({
        message: true
      })
    }, 500)
  },
  //点击跳转到首页的列表
  lookFang: function (e) {
    let id = e.currentTarget.dataset.preid;
    wx.navigateTo({
      url: '/pages/index/propertyDetailA/propertyDetailA?preId=' + id + '&isShared=no'
    })
  }
})